﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;

namespace LGPSDataGen.Generators
{
    class GenericWeatherGenerator : Generator, IGenerator
    {

        protected string pathToFile;
        private string content = "";
        //The Open source is made up of the header, the date, and the tail
        private string header = "http://www.wunderground.com/history/airport/KLOM/";
        private string tail = "/DailyHistory.html?req_city=Philadelphia&req_state=PA&req_statename=Pennsylvania&format=1";
        private string date = "";

        public GenericWeatherGenerator(DateTime theDate)
        {
            date = theDate.Year + "_" + theDate.Month + "_" + theDate.Day;
            content = header + date.Replace("_", "/") + tail; //file to grab (dynamic to date)
            pathToFile = @"Temperature_" + date + "_.txt"; //download file
        }

        public override void DownloadData()
        {
            WebClient wc = new WebClient();
            wc.DownloadFile(content, pathToFile);
        }

        public override void ExtractData()
        {
            ReplaceHTML_BR();
        }

        /// <summary>
        /// First line is blank
        /// Second line is header
        /// HH:MM:SS AM, Temperature
        /// </summary>
        public override void ProcessData()
        {
            System.Console.WriteLine("This needs to be overridden!");
        }

        protected void SkipTopTwoLines(StreamReader fileToOpen)
        {
            if (!fileToOpen.EndOfStream)
                fileToOpen.ReadLine();
            if (!fileToOpen.EndOfStream)
                fileToOpen.ReadLine();
        }

        //No longer needed
        protected void AdvanceToNextHour(StreamReader file)
        {
            //SkipTopTwoLines(file);
            //if (!file.EndOfStream)
            //    file.ReadLine();
        }

        //Make it so the data becomes human readable
        //otherwise tough to debug and evaluate
        private void ReplaceHTML_BR()
        {
            string fileText = ReadFile();
            WriteFile(fileText);
        }

        private string ReadFile()
        {
            StreamReader fileToOpen = new StreamReader(pathToFile);
            string fileContents = fileToOpen.ReadToEnd();
            fileToOpen.Close();

            return fileContents;
        }

        private void WriteFile(string textToWrite)
        {
            //Create csv in human readable form
            pathToFile = pathToFile + ".csv";
            StreamWriter fileToCreate = new StreamWriter(pathToFile);
            fileToCreate.Write(ExtractBR(textToWrite));
            fileToCreate.Close();
        }

        //<br> html flag should become a new line only (otherwise it adds clutter)
        private string ExtractBR(string textToFilter)
        {
            return textToFilter.Replace("<br />", "");
        }

    }
}
